# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont
import time


class ChaiMatchImg:
    def __init__(self):
        self.BORDER_COLOR = "#e0e0e0"
        self.TITLE_COLOR = "#1a1a1a"
        self.SUB_TITLE_COLOR = "#888888"
        self.TH_BG_COLOR = "#b2301f"
        self.PRE_THREE_BG_COLOR = "#982516"
        self.TH_FONT_COLOR = "#ffffff"
        self.TD_COMMON_COLOR = "#333333"
        self.COPY_COLOR = "#cccccc"
        self.BORDER_WIDTH = 2
        self.TITLE_PART_HEIGHT = 130
        self.TD_HEIGHT = 32
        self.TD_V_OFFSET = 5
        self.TH_W_ARR = [72, 200, 80, 72, 72, 72, 72]
        self.TH_TITLE_ARR = ["序号", "昵称", "速度", "击键", "码长", "回改", "错字"]
        self.imgW = 640
        self.imgH = 400

    def drawTextArr(self, draw, text_arr, pos_arr, fill_color, hIndex, isNotHead):
        for i in range(0, len(text_arr)):
            colY = pos_arr[i]
            tfont = ImageFont.truetype("msyh.ttc", 16)
            w, h = draw.textsize(text_arr[i], font=tfont)
            y = colY - (self.TH_W_ARR[i] + w)/2
            if i == 1 and isNotHead:
                y = colY - self.TH_W_ARR[i] + 14
            draw.text((self.BORDER_WIDTH + y, self.TITLE_PART_HEIGHT + self.TD_V_OFFSET + hIndex * self.TD_HEIGHT), text_arr[i], fill=fill_color, font=tfont)


    def draw(self, result_title, result_arr, pngName):
        start = time.time()
        resultArrLen = len(result_arr)
        self.imgH = self.TITLE_PART_HEIGHT + self.TD_HEIGHT * (resultArrLen + 2) + self.BORDER_WIDTH * 2
        matchTitle = result_title
        createText = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))

        img = Image.new("RGB", (self.imgW, self.imgH), "#FFFFFF")

        draw = ImageDraw.Draw(img)

        tfont = ImageFont.truetype("msyh.ttc", 20)
        w, h = draw.textsize(matchTitle, font=tfont)
        draw.text(((self.imgW - w) / 2, 40), matchTitle, fill=self.TITLE_COLOR, font=tfont)

        tfont = ImageFont.truetype("msyh.ttc", 16)
        w, h = draw.textsize(createText, font=tfont)
        draw.text((self.imgW - w - 16, 102), createText, fill=self.SUB_TITLE_COLOR, font=tfont)
        draw.text((self.imgW - w - 118, self.TITLE_PART_HEIGHT +  self.TD_HEIGHT * (resultArrLen + 1) + self.TD_V_OFFSET + 2), "小拆成绩单 ©2022 Zhiyihu、Dada", fill=self.COPY_COLOR, font=tfont)

        draw.line(
            [(self.BORDER_WIDTH, self.TITLE_PART_HEIGHT), (self.imgW - self.BORDER_WIDTH, self.TITLE_PART_HEIGHT)],
            fill=self.BORDER_COLOR, width=1)

        draw.rectangle(
            [(self.BORDER_WIDTH, self.BORDER_WIDTH), (self.imgW - self.BORDER_WIDTH, self.imgH - self.BORDER_WIDTH)],
            fill=None, outline=self.BORDER_COLOR)
        draw.rectangle([(self.BORDER_WIDTH, self.TITLE_PART_HEIGHT),
                        (self.imgW - self.BORDER_WIDTH, self.TITLE_PART_HEIGHT + self.TD_HEIGHT)],
                       fill=self.TH_BG_COLOR, outline=None)
        colY = 0
        posYArr = []
        for i in range(0, len(self.TH_W_ARR)):
            colY += self.TH_W_ARR[i]
            posYArr.append(colY)

        self.drawTextArr(draw, self.TH_TITLE_ARR, posYArr, self.TH_FONT_COLOR, 0, False)

        for i in range(0, min(resultArrLen, 3)):
            draw.rectangle([(self.BORDER_WIDTH, self.TITLE_PART_HEIGHT + self.TD_HEIGHT * (i + 1)),
                            (self.imgW - self.BORDER_WIDTH, self.TITLE_PART_HEIGHT + self.TD_HEIGHT * (i + 2))],
                           fill=self.PRE_THREE_BG_COLOR, outline=None)
            trLineArr = [str(i+1), result_arr[i]["nickname"], result_arr[i]["speed"], result_arr[i]["keySpeed"], result_arr[i]["codeLen"], result_arr[i]["backCount"],  result_arr[i]["errCount"]]
            self.drawTextArr(draw, trLineArr, posYArr, self.TH_FONT_COLOR, i + 1, True)
        if resultArrLen > 3:
            for i in range(3, resultArrLen):
                draw.line([(self.BORDER_WIDTH, self.TITLE_PART_HEIGHT + self.TD_HEIGHT * (i + 2)),
                                (self.imgW - self.BORDER_WIDTH, self.TITLE_PART_HEIGHT + self.TD_HEIGHT * (i + 2))],
                            fill=self.BORDER_COLOR, width=1)
                trLineArr = [str(i+1), result_arr[i]["nickname"], result_arr[i]["speed"], result_arr[i]["keySpeed"], result_arr[i]["codeLen"], result_arr[i]["backCount"],  result_arr[i]["errCount"]]
                self.drawTextArr(draw, trLineArr, posYArr, self.TD_COMMON_COLOR, i + 1, True)

            for i in range(0, len(self.TH_W_ARR) - 1):
                colY = posYArr[i]
                draw.line([(self.BORDER_WIDTH + colY, self.TITLE_PART_HEIGHT + self.TD_HEIGHT * (3 + 1)+1),
                                (self.BORDER_WIDTH + colY, self.TITLE_PART_HEIGHT + self.TD_HEIGHT * (resultArrLen+1))],
                            fill=self.BORDER_COLOR, width=1)

        img.save(pngName + ".png")

        end = time.time()
        print("cost: ", "{:.3}".format(end - start) + "s")


if __name__ == '__main__':
    chaiMathImg = ChaiMatchImg()
    groupTitle = "小鹤双拼练习1"
    resultArr = [{
        "nickname": "栉异乎",
        "speed": "204.32",
        "keySpeed": "8.92",
        "codeLen": "2.43",
        "backCount": "42",
        "errCount": "0"
    }]
    imgFileName = "xiaohe1"
    chaiMathImg.draw(groupTitle, resultArr, imgFileName)
